<template>
  <div class="table-box">
    <ProTable ref="pTable" :columns="columns" :init-param="initParam" :request-api="getTableList">
      <template #tableHeader>
        <el-button type="primary" :icon="CirclePlus" @click="handleOpenDialog(OptEnum.CREATE)"> 新增 </el-button>
      </template>
      <template #flagDefault="scope">
        <el-tag :type="scope.row.flagDefault === activeEnum.ACTIVE ? 'primary' : 'info'">
          {{ scope.row.flagDefault === activeEnum.ACTIVE ? "默认" : "--" }}
        </el-tag>
      </template>
      <template #operation="scope">
        <el-button type="primary" size="small" :icon="EditPen" @click="handleOpenDialog(OptEnum.EDIT, scope.row)">修改</el-button>
        <el-button type="danger" size="small" :icon="Delete" @click="handleDeleteItem(scope.row)">删除</el-button>
      </template>
    </ProTable>
    <DealerCardDialog ref="dialogRef" @refresh-list="handleRefreshList" />
  </div>
</template>
<script setup lang="ts">
import { ref, reactive } from "vue";
import ProTable from "@/components/ProTable/index.vue";
import { CirclePlus, Delete, EditPen } from "@element-plus/icons-vue";
import { getDealerCardVoListApi, deleteDealerCardApi } from "@/api/modules/dealer/card";
import { ColumnProps } from "@/components/ProTable/interface";
import { useHandleData } from "@/hooks/useHandleData";
import { OptEnum, activeEnum } from "@/biz/eenum/appEnum";

import { DealerCardVo } from "@/biz/interface/modules/DealerCardType";
import DealerCardDialog from "./components/DealerCardDialog.vue";
const pTable = ref();
// 固定在列表中的参数
const initParam = reactive({});
// 默认不做操作就直接在 ProTable 组件上绑定	:requestApi="getUserList"
const getTableList = (params: any) => {
  // 这边可以处理一下参数
  return getDealerCardVoListApi(params);
};
// 表格列配置
const columns: Partial<ColumnProps>[] = [
  { type: "index", label: "#", width: 60 },
  {
    prop: "cardNo",
    label: "卡号",
    search: { el: "input" }
  },
  {
    prop: "cardName",
    label: "开户人"
  },
  {
    prop: "bankName",
    label: "开户银行"
  },
  {
    prop: "flagDefault",
    label: "默认"
  },
  {
    prop: "createDate",
    label: "申请时间"
  },
  {
    prop: "operation",
    width: 200,
    fixed: "right",
    label: "操作"
  }
];

//刷新数据列表
const handleRefreshList = () => {
  pTable.value.getTableList();
};

// 删除信息
const handleDeleteItem = async (params: DealerCardVo) => {
  await useHandleData(deleteDealerCardApi, { id: params.id! }, `删除【${params.cardNo}】信息`);
  pTable.value.getTableList();
};

// 打开 dialog(新增、查看、编辑)
const dialogRef = ref();
const handleOpenDialog = (optType: OptEnum, rowData?: { id: string }) => {
  let params = {
    optType: optType,
    rowData: { ...rowData }
  };
  dialogRef.value.acceptParams(params);
};
</script>
